<?php

namespace App\Admin\Controllers;

use App\Models\ToolCabinetModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\AddressModel;

class ToolCabinetController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '研学工具箱';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ToolCabinetModel());

        $grid->column('id', __('Id'));
        $grid->column('title','标题');
       // $grid->column('content', __('Content'));
        $grid->column('country','国家');
       // $grid->column('line_id', __('Line id'));
        //$grid->column('line', __('Line'));
       // $grid->column('city', __('City'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ToolCabinetModel::findOrFail($id));
        //var_dump($show->content());die;

        $show->field('id', __('Id'));
        //$show->field('title', __('Title'));
       // $show->title()->label();

        //$show->editor('content','内容');

        $show->field('country','国家');
       // $show->field('line_id', __('Line id'));
        //$show->field('line', __('Line'));
      //  $show->field('city', __('City'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ToolCabinetModel());

        $form->text('title','内容');

       // $form->textarea('content', __('Content'));
        $form->editor('content','内容');

        //$form->text('country', __('Country'));

        $form->select('country_id','請選擇国家')
            //->options(AddressModel::where('parent_id',0)->pluck('title','id'))
            ->options( '/admin/get_country')
            ->rules('required',['required' => '請選擇国家']);

        $form->hidden('country','国家')->disable();
        $form->saving(function (Form $form) {
            $form->country =  AddressModel::where('id',$form->country_id)->value('address');
        });

        return $form;
    }

    /**
     * 获取国家
     * @return array
     */
    public function get_country()
    {

        $AddressModel = new AddressModel();
        $ToolCabinetModel = new ToolCabinetModel();
        $Tool = $ToolCabinetModel->select(['country_id'])->get()->toArray();

        if(count($Tool)>0){

            foreach ($Tool as $k=>$v){

                $Tool[$k] = $v['country_id'];
            }

        }else{

            $Tool = [];

        }

        $data = $AddressModel->select(['id', 'address as text', 'parent_id'])
            ->where('parent_id','=',0)
            //->whereNotIn('id',$Tool)
            ->get()
            ->toArray();
        //$data = getTree($data,$q);
        // array_unshift($data,['id'=>0,'text'=>'顶级分类']);
        return $data;
    }
}
